package com.train.mgr.config.secrity.components;

import com.train.common.enums.LoginTypeEnum;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

import java.util.Collection;

public class TrainAuthenticationToken extends AbstractAuthenticationToken {
    //认证主体，这里用来存手机号
    private final Object principal;
    //凭证，一般用来存密码，我这里用来存验证码
    private Object credentials;

    //用户类型
    private Integer userType;
    //OpenID
    private String openId;
    private String userId;
    private String token;
    //登录方式：1、微信登录，2、账号密码登录
    private LoginTypeEnum loginType;

    //初始化的构造方法
    public TrainAuthenticationToken(Object principal, Object credentials , Integer userType , String openId , String token , LoginTypeEnum loginType) {
        super((Collection)null);
        this.principal = principal;
        this.credentials = credentials;
        this.userType = userType;
        this.openId = openId;
        this.token = token;
        this.loginType = loginType;
        this.setAuthenticated(false);
    }

    //认证完成后调用的构造方法
    public TrainAuthenticationToken(Object principal, Object credentials, Integer userType , String openId , String userId, String token, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.principal = principal;
        this.credentials = credentials;
        this.userType = userType;
        this.userId = userId;
        this.token = token;
        this.openId = openId;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return this.credentials;
    }

    @Override
    public Object getPrincipal() {
        return this.principal;
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        Assert.isTrue(!isAuthenticated, "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        super.setAuthenticated(false);
    }

    public void eraseCredentials() {
        super.eraseCredentials();
        this.credentials = null;
    }

    public Integer getUserType() {
        return userType;
    }

    public void setUserType(Integer userType) {
        this.userType = userType;
    }

    public String getOpenId() {
        return openId;
    }

    public String getUserId() {
        return userId;
    }
    public String getToken() {
        return token;
    }

    public void setOpenID(String OpenId) {
        OpenId = OpenId;
    }

    public LoginTypeEnum getLoginType() {
        return loginType;
    }

    public void setLoginType(LoginTypeEnum loginType) {
        this.loginType = loginType;
    }

}
